package cn.yu.dream.web.socket.biz;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.env.Environment;

import java.util.Objects;
import java.util.Properties;

@EnableDiscoveryClient
@SpringBootApplication
public class DreamWebSocketApplication {

	public static void main(String[] args) throws NacosException {
		SpringApplication.run(DreamWebSocketApplication.class, args);
		Environment env = SpringUtil.getBean(Environment.class);
		NacosDiscoveryProperties nacosProperties = SpringUtil.getBean(NacosDiscoveryProperties.class);

		Properties properties = new Properties();
		properties.setProperty("serverAddr", nacosProperties.getServerAddr());
		properties.setProperty("namespace", nacosProperties.getNamespace());
		NamingService naming = NamingFactory.createNamingService(properties);
		naming.registerInstance(env.getProperty("spring.application.name"), "0.0.0.0",
				Integer.parseInt(Objects.requireNonNull(env.getProperty("server.port"))), "DEFAULT");
	}

}
